//
//  KBSelectedWorks.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "KBSelectedWorks.h"

@interface KBSelectedWorks ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, assign)int i;

@property (nonatomic, assign)int countNum;

@property (nonatomic, assign)NSInteger numColor; // 用来改变item按钮颜色

@end

@implementation KBSelectedWorks

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.i = 0;

        self.frame = CGRectMake(SCREEN_WIDTH*2, 45, SCREEN_WIDTH, SCREEN_HEIGHT - 290);
        self.backgroundColor = [UIColor colorWithRed:0.31 green:0.31 blue:0.31 alpha:0.7];

        UICollectionViewFlowLayout *myLayout = [[UICollectionViewFlowLayout alloc] init];
        myLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 4 - 15, 30);
        
        // 间距
        myLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        // 列间距
        myLayout.minimumLineSpacing = 10;
        // 滚动方向
        myLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.myCollectV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _count/80*40+80) collectionViewLayout:myLayout];

        self.myCollectV.backgroundColor = [UIColor whiteColor];
        self.myCollectV.delegate = self;
        self.myCollectV.dataSource = self;
        [self addSubview:self.myCollectV];
        
        // 注册
        [self.myCollectV registerClass:[KBselectedCell class] forCellWithReuseIdentifier:@"cell"];
        
    }
    return self;
}

- (void)setCount:(int)count
{
    // 计算行数
    NSInteger aaa = count/80;
    _countNum = count;
    if (count>aaa*80)
    {
        _myCollectV.frame = CGRectMake(0, 0, SCREEN_WIDTH, (aaa+1)*40+10);
    }
//    NSLog(@"行数 = %d", count/80);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _maxPageId;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    KBselectedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    if (_count == 1)
    {
        cell.str = [NSString stringWithFormat:@"1"];
    }
    else
    {
        int num = (self.i+1)*20;
        if (indexPath.row != _maxPageId -1)
        {
            _i++;
            cell.str = [NSString stringWithFormat:@"%d~%d", num-20+1, num];
        }
        if (indexPath.row == _maxPageId-1)
        {
            cell.str = [NSString stringWithFormat:@"%d~%ld", num-20+1, (long)_countNum];
        }

        if (indexPath.row == _numColor)
        {
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.39 blue:0.26 alpha:1];
            cell.text.textColor = [UIColor colorWithRed:0.98 green:0.95 blue:0.91 alpha:1];
        }
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.SWdelegate selectedDelegate:indexPath GetData:_maxPageId];
//    NSLog(@"12123213123312 === %@", indexPath);
    // 获取到cell中的第一个row
    KBselectedCell *cell = (KBselectedCell *)[_myCollectV cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_numColor inSection:0]];
    // 改变回默认颜色
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1];
    cell.text.textColor = [UIColor blackColor];

    cell = (KBselectedCell *)[_myCollectV cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.39 blue:0.26 alpha:1];
    cell.text.textColor = [UIColor colorWithRed:0.98 green:0.95 blue:0.91 alpha:1];
    
    _numColor = indexPath.row;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
